package servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import utils.DBUtil_Plus;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/group/equipment/Equipment_save")
public class EquipmentSave extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        String eq_id = req.getParameter("id");
        String eq_name = req.getParameter("eq_name");
        String eq_number = req.getParameter("eq_number");
        String eq_type = req.getParameter("eq_type");
        String eq_producer = req.getParameter("eq_producer");
        String eq_data = req.getParameter("eq_data");
        String eq_store_data = req.getParameter("eq_store_data");
        String eq_after_sale = req.getParameter("eq_after_sale");
        String eq_phone = req.getParameter("eq_phone");
        String eq_introduction = req.getParameter("eq_introduction");
        Connection conn = null;
        try {
            conn = DBUtil_Plus.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if(eq_id.equals("0")){
            // 这里用的是Request的请求方式 所以获取到的是String类型的
            // 上面忘记转换了这里就需要这样来判断
            // 这里的eq_id是字符串的null String eq_id = "null" 而不是String eq_id = null 可能是因为
            // 在页面中传递了两次，第一次是第二种情况，但是再传递一个的话就是第一种情况了
            int eq_idd = 0;
            String query = "SELECT id FROM eq_list ORDER BY id DESC LIMIT 1";
            PreparedStatement pstmt_query = null;
            try {
                pstmt_query = conn.prepareStatement(query);
                ResultSet rs_query = null;
                rs_query = pstmt_query.executeQuery();
                if(rs_query.next()){
                    eq_idd = rs_query.getInt("id");
                    eq_idd++;
                }else{

                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            String sql1  = "insert into eq_list (id,eq_name,eq_number,eq_type,eq_producer,eq_data,eq_store_data,eq_after_sale,eq_phone,eq_introduction) values (?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt1 = null;
            try {
                pstmt1 = conn.prepareStatement(sql1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                pstmt1.setInt(1,eq_idd);
                pstmt1.setString(2,eq_name);
                pstmt1.setString(3,eq_number);
                pstmt1.setString(4,eq_type);
                pstmt1.setString(5,eq_producer);
                pstmt1.setString(6,eq_data);
                pstmt1.setString(7,eq_store_data);
                pstmt1.setString(8,eq_after_sale);
                pstmt1.setString(9,eq_phone);
                pstmt1.setString(10,eq_introduction);
                int rs = pstmt1.executeUpdate();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }else{
            String update_message = "update eq_list set eq_name = ?,eq_number = ?,eq_type = ?,eq_producer = ?,eq_data = ?,eq_store_data = ?,eq_after_sale = ?,eq_phone = ?,eq_introduction = ? where id = ?";
            PreparedStatement pstmt = null;
            try {
                try {
                    pstmt = conn.prepareStatement(update_message);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                pstmt.setString(1, eq_name);
                pstmt.setString(2, eq_number);
                pstmt.setString(3, eq_type);
                pstmt.setString(4, eq_producer);
                pstmt.setString(5, eq_data);
                pstmt.setString(6, eq_store_data);
                pstmt.setString(7, eq_after_sale);
                pstmt.setString(8, eq_phone);
                pstmt.setString(9, eq_introduction);
                pstmt.setInt(10, Integer.parseInt(eq_id));
                int rs = pstmt.executeUpdate();
            }catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        DBUtil_Plus.closeConnection(conn);
        resp.setHeader("refresh","0;URL="+"Equipment_list");
    }
}
